import {AbstractRoom} from "../core/room/AbstractRoom";
import {Client} from "../core/Client";

export class CockyRoom extends AbstractRoom {
  public maxClients: number = 2;
  private msgHandler: Map<string, Function> = new Map<string, Function>();
  private cockyInfo = {}

  constructor() {
    super();
    this.msgHandler.set('pos', this.onSyncPos.bind(this))
  }

  onDispose(): void | Promise<any> {
    return undefined;
  }

  onInit(options: any): void {
  }

  onJoin(client: Client): void | Promise<any> {
    return undefined;
  }

  onLeave(client: Client): void | Promise<any> {
    return undefined;
  }

  onMessage(client: Client, res: { $router: string, data: any }): void {
    const func = this.msgHandler.get(res.$router)
    if (func) {
      func(res.data, client);
    }
  }

  onSyncPos(data, client: Client) {
    this.cockyInfo[client.id] = data;
  }

  gg = true

  onFrame(dt: number): void {
    if (this.gg) {
      this.gg = false;
      this.broadcast('pos', this.cockyInfo)
    }
  }

}